package com.aynu.webshop.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.User;
import com.aynu.webshop.web.pagemodel.PageModel;
import com.aynu.webshop.web.service.AddressService;
import com.aynu.webshop.web.service.UserService;
import com.aynu.webshop.web.util.FinalVarUtil;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Resource
	private UserService userService;
	@Resource
	private AddressService addressService;
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：获取所有用户信息
	 */
	@RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
	public ModelAndView getAllUser(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<User> list = userService.selectList();
		String th_list = FinalVarUtil.UserTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("user/userList");
		return mv;
	}
	
	
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到登录页面 
	 */
	@RequestMapping(value = "/loginJsp", method = RequestMethod.GET)
	public ModelAndView loginJsp(HttpServletRequest request ,User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("../../login");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：普通用户登录 
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		User confirm = userService.findByUname(user.getuName());
		if (confirm != null) {
			System.out.println(confirm);
			if (confirm.getuPassword().equals(user.getuPassword())) {
			//	request.getSession().setAttribute("user", confirm);
				if (confirm.getuStatus()==0) {
					//失效用户做失效提示
					System.out.println("失效用户做失效提示");
				}
				if (confirm.getuStatus()==1) {
					request.getSession().setAttribute("user", confirm);
					//正常用户跳到首页
					System.out.println("正常用户跳到首页");
					mv.setViewName("../../index");
				}
			} else {
			//	request.setAttribute("failedInfo", "登录失败,密码错误！");
				System.out.println("登录失败，用户名或密码错误");
				mv.setViewName("../../login");
			}
		} else {
		//	request.setAttribute("failedInfo", "登录失败,没有该用户！");
			System.out.println("登录失败");
			mv.setViewName("../../login");
		}
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到注册页面 
	 */
	@RequestMapping(value = "/registerJsp", method = RequestMethod.GET)
	public ModelAndView registerJsp(HttpServletRequest request ,User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/register");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：注册 
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView register(HttpServletRequest request ,User user) {
		ModelAndView mv = new ModelAndView();
		User confirm = userService.findByUname(user.getuName());
		
		if(user!=null){//如果用户信息不为空
			if (confirm != null) {//如果根据用户名查出有该用户
				//注册失败 已有该用户
			}else{//注册新用户
				userService.insert(user);
			}
		}
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：修改用户信息 
	 */
	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	public ModelAndView editUser(HttpServletRequest request ,User user) {
		ModelAndView mv = new ModelAndView();
		userService.update(user);
		User user2 =userService.selectById(user.getuId());
		mv.addObject("user",user2);
		mv.setViewName("user/myinfoto");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：注销用户
	 */
	@RequestMapping(value = "/cancelUser", method = RequestMethod.GET)
	public ModelAndView cancelUser(HttpServletRequest request,Integer uid) {
		ModelAndView mv = new ModelAndView("redirect:getAllUser");
		User user = userService.selectById(uid);
		user.setuStatus(0);
		userService.update(user);
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：删除用户
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
	public ModelAndView deleteUser(HttpServletRequest request,Integer uid) {
		ModelAndView mv = new ModelAndView("redirect:getAllUser");
		userService.delete(uid);
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：登出 
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		request.getSession().removeAttribute("user");
		mv.setViewName("../../login");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我的个人信息 
	 */
	@RequestMapping(value = "/myInfo", method = RequestMethod.GET)
	public ModelAndView myInfo(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		System.out.println("myInfo界面！");
		User user = (User) request.getSession().getAttribute("user");
		System.out.println("userInfo"+user);
		User user2 =userService.selectById(user.getuId());
		mv.addObject("user",user2);
		mv.setViewName("user/myinfoto");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我的地址信息 
	 */
	@RequestMapping(value = "/myAddress", method = RequestMethod.GET)
	public ModelAndView myAddress(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		System.out.println("myInfo界面！");
		User user = (User) request.getSession().getAttribute("user");
		mv.addObject("user",user);
		mv.setViewName("user/myAddress");
		return mv;
	}
}
